﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace inohigo
{
	/// <summary>
	/// 一般的な再帰下降構文解析器
	/// </summary>
	public class RecursiveDescentParser
	{
		int position = 0;
		AST root;

		public RecursiveDescentParser() { }

		public AST Parse( List<Token> tokenList )
		{
			root = new AST();

			while( position < tokenList.Count )
			{
				parse( tokenList );
			}
			return root;
		}

		private void parse( List<Token> tokenList )
		{
			AST ast = new AST();
			Token token = tokenList[position];

			if( token.TokenType == TokenType.NUMBER )
			{
				Token next = tokenList[position + 1];
				if( next.TokenType == TokenType.MINUS_OP ||
					next.TokenType == TokenType.MUL_OP ||
					next.TokenType == TokenType.ADD_OP ||
					next.TokenType == TokenType.DIV_OP )
				{
					AST left = new AST();
					left.Value = next;
					ast.Left = left;
				}
				else // 1 2 => ?
				{
					throw NotImplementedException( next );
				}
			}

			//if( token.TokenType == TokenType.ADD_OP )
			//{
			//    root.Value = token;
			//    position++;
			//    parse( tokenList );
			//}
			//else if( token.TokenType == TokenType.DIV_OP)
		}

		private Exception NotImplementedException( Token next )
		{
			Console.WriteLine( "Unknow next( TokenType:{0}, TokenValue: {1} )", next.TokenType.ToString(), next.Value.ToString() );
			Console.WriteLine( "Press any key..." );
			Console.ReadKey();
			Environment.Exit( 1 );
			throw new NotImplementedException();
		}
	}
}
